package computer.controllers;

import java.io.IOException;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;

import jakarta.servlet.annotation.WebServlet;
import computer.business.*;
import computer.data.*;

@WebServlet("/email/product/*")
public class EmailController extends HttpServlet {
    
    @Override
    public void doGet(HttpServletRequest request, 
            HttpServletResponse response)
            throws ServletException, IOException {

        String url;

        url = showProduct(request, response);
        getServletContext()
            .getRequestDispatcher(url)
            .forward(request, response);
    }
    
    private String showProduct(HttpServletRequest request, 
            HttpServletResponse response) {
        /*66666666666666666666666666666666666*/
        String productCode = request.getPathInfo();
        // This should never be null. But just to be safe.
        if (productCode != null) {
            productCode = productCode.substring(1);
            Product product = ProductDB.selectProduct(productCode);
            HttpSession session = request.getSession();
            session.setAttribute("product", product);
        }
        return "/catalog/" + productCode + "/index.jsp";
    }

}